
import javax.crypto.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    private String[] name;
    private String[] location;
    private int numConnections;
    private int port;
    private String myName;
    public Socket socket;
    public BufferedReader receive;
    public PrintWriter send;

    public static void main(String[] args) {
		/*
        int myId = Integer.parseInt(args[0]);
        ReadFile systemFile = new ReadFile(new File("system.properties")); // read in file and get system info
        this.numConnections = systemFile.getNumConnections();
        this.name = new String[numConnections];
        this.location = new String[numConnections];
        this.name = systemFile.getName();
        this.location = systemFile.getLocation();
        this.port = systemFile.getPort();
        ServerSocket listenSocket = new ServerSocket(this.port);
        
        for(int i=0; i<this.numConnections;i++){
            if(i==myId){
                this.myName = this.name[i];
            }
        }
        
        if(this.myName.equals("Alice")){
        this.socket = new Socket(this.location[bob], this.port);
        this.receive = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        this.send = new PrintWriter(this.socket.getOutputStream(), true);
        }    
        
        if(this.myName.equals("Bob")){
        this.socket = listenSocket.accept();;
        this.receive = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        this.send = new PrintWriter(this.send.getOutputStream(), true);
        }   
		*/
		try
		{
			//setup variables requied for DES
			SecretKey k1 = KeyGenerator.getInstance("DES").generateKey();
			SecretKey k2 = KeyGenerator.getInstance("DES").generateKey();
			
			byte[] cipher;
			byte[] text;
				
			String getInputString = "Enter the sequence to be encrypted\n";
			String getIVString = "Enter the IV (must be 8 bytes)\n";
			String getKeyString = "Enter key\n";
				
			System.out.println("CNT5410 Project Part 1");   
			System.out.println("Please select encryption method:");
			System.out.println("1. 3DES only (max block size is 8 bytes)");
			System.out.println("2. 3DES/CFB");
			System.out.println("3. 3DES/CBC (Integrity Only)");
			System.out.println("4. 3DES/PCBC");
			System.out.println("5. RC4");
			System.out.println("6. RSA");
			
			System.out.print("Enter selection: ");
			String opt = System.console().readLine();
                        
			//do 3DES
			if (opt.equals("1"))
			{
				System.out.println("Encryption Type: 3DES");  
				
				PrintKeys(k1, k2);
				
				String input = Utils.GetUserInput(getInputString);
				
				byte[] byteArray = input.getBytes(); 

				TripleDES tdes = new TripleDES();
				
				System.out.println("clearText: " + input);
				cipher = tdes.Encrypt(k1, k2, byteArray);
				System.out.println("cipher: " + Utils.ByteToString(cipher));
				
				text = tdes.Decrypt(k1, k2, cipher);
				System.out.println("clearText: " + Utils.ByteToString(text));
			}
			//do 3DES/CFB
			else if (opt.equals("2"))
			{
				System.out.println("Encryption Type: 3DES/CFB");  
				
				//PrintKeys(k1, k2);
				
				String key = Utils.GetUserInput(getKeyString);
				byte[] IV = Utils.GetUserInput(getIVString).getBytes();
				String input = Utils.GetUserInput(getInputString);
				byte[] byteArray = input.getBytes(); 
			
				CFB cfb = new CFB();
				
				//System.out.println("clearText: " + input);
				cipher = cfb.Encrypt(cfb.GetKeyFromString(key), IV, byteArray);
				System.out.println("cipher: " + Utils.ToHexString(cipher));
				
				text = cfb.Decrypt(cfb.GetKeyFromString(key), IV, cipher);
				System.out.println("clearText: " + Utils.ByteToString(text));
			}
			
			//do 3DES/CBC
			else if (opt.equals("3"))
			{
				System.out.println("Encryption Type: 3DES/CBC");  
				
				//PrintKeys(k1, k2);
				
				String key = Utils.GetUserInput(getKeyString);
				byte[] IV = Utils.GetUserInput(getIVString).getBytes();
				String input = Utils.GetUserInput(getInputString);
				byte[] byteArray = input.getBytes(); 
				

				CBC cbc = new CBC();
				
				//System.out.println("clearText: " + input);
				cipher = cbc.GetMac(cbc.GetKeyFromString(key), IV, byteArray);
				System.out.println("MAC (last 8 bytes of ciphertext): " + Utils.ByteToString(cipher));
			}
			
			//do 3DES/PCBC
			else if (opt.equals("4"))
			{
				/*
				System.out.println("Encryption Type: 3DES/PCBC");  
				
				System.out.println("Keys for MAC");
				PrintKeys(k1, k2);
				
				String input = Utils.GetUserInput(getInputString);
				byte[] byteArray = input.getBytes(); 
				byte[] IV = Utils.GetUserInput(getIVString).getBytes();

				PCBC pcbc = new PCBC();
				
				System.out.println("clearText Before encryption: " + input);
				cipher = pcbc.GetMac(k1, k2, IV, byteArray);
				
				String inputNmac = input + Utils.ByteToString(cipher);
				System.out.println("cleartext + mac: " + inputNmac);
				
				SecretKey k11 = KeyGenerator.getInstance("DES").generateKey();
				SecretKey k22 = KeyGenerator.getInstance("DES").generateKey();
				
				System.out.println("Keys for Encryption");
				PrintKeys(k11, k22);
				
				//encrypt message and MAC
				cipher = pcbc.Encrypt(k11, k22, IV, inputNmac.getBytes());
				
				System.out.println("cipher: " + Utils.ByteToString(cipher));
				
				//decrypt
				text = pcbc.Decrypt(k11, k22, IV, cipher);
				System.out.println("clearText + mac after decryption: " + Utils.ByteToString(text));
				
				String temp = Utils.ByteToString(text);
				String mac = temp.substring(input.length(), temp.length());
				
				System.out.println("mac in decrypted message: " + mac);
				
				temp = temp.substring(0, input.length());
				
				//get mac of decrypted message to compare with current mac to check integrity
				cipher = pcbc.GetMac(k1, k2, IV, temp.getBytes());
				
				System.out.println("mac produced by encrypting the received message: " + Utils.ByteToString(cipher));
				*/
			}
			//do RC4
			else if (opt.equals("5"))
			{
                            String input = Utils.GetUserInput(getInputString);
			
			    byte[] byteArray = input.getBytes(); 
                            System.out.println("clearText: " + input);
                            RC4 rc4 = new RC4(byteArray);
                            
			}
			
			//do RSA
			else if (opt.equals("6"))
			{	/*
				String input = Utils.GetUserInput(getInputString);
			
				byte[] byteArray = input.getBytes(); 
                                System.out.println("clearText: " + input);
				RsaEncryption modeOne = new RsaEncryption(13, 19, 5, byteArray);
				
				RsaEncryption modeTwo = new RsaEncryption(11, 23, 3, byteArray);*/
			}
			
			else 
				System.out.println("Entered value not valid.");
		}
		catch (Exception e) { e.printStackTrace(); } 
	}
	
	public static void PrintKeys(SecretKey k1, SecretKey k2) throws Exception
	{
		System.out.println();
		
		byte[] bkey = k1.getEncoded();
		System.out.print("Key 1: ");
		System.out.println(Utils.ToHexString(bkey));
		
		bkey = k2.getEncoded();
		System.out.print("Key 2: ");
		System.out.println(Utils.ToHexString(bkey));
		
		System.out.println();
	}
}













